﻿namespace WinnerCMS.Common.Security
{
    using System;
    using System.Globalization;
    using System.Text;

    public static class Encoder
    {
        private const string JavaScriptEmptyString = "''";
        private static readonly char[][] SafeListCodes = InitializeSafeList();
        private const string VbScriptEmptyString = "\"\"";

        public static string CssEncode(string input)
        {
            return CssEncoder.Encode(input);
        }

        public static string HtmlAttributeEncode(string input)
        {
            return UnicodeCharacterEncoder.HtmlAttributeEncode(input);
        }

        public static string HtmlEncode(string input)
        {
            return HtmlEncode(input, false);
        }

        public static string HtmlEncode(string input, bool useNamedEntities)
        {
            return UnicodeCharacterEncoder.HtmlEncode(input, useNamedEntities);
        }

        public static string HtmlFormUrlEncode(string input)
        {
            return HtmlFormUrlEncode(input, Encoding.UTF8);
        }

        public static string HtmlFormUrlEncode(string input, int codePage)
        {
            return HtmlFormUrlEncode(input, Encoding.GetEncoding(codePage));
        }

        public static string HtmlFormUrlEncode(string input, Encoding inputEncoding)
        {
            if (inputEncoding == null)
            {
                inputEncoding = Encoding.UTF8;
            }
            return HtmlParameterEncoder.FormStringParameterEncode(input, inputEncoding);
        }

        private static char[][] InitializeSafeList()
        {
            char[][] chArray = new char[0x10000][];
            for (int i = 0; i < chArray.Length; i++)
            {
                if ((((((i >= 0x61) && (i <= 0x7a)) || ((i >= 0x41) && (i <= 90))) || (((i >= 0x30) && (i <= 0x39)) || ((((i == 0x20) || (i == 0x2e)) || ((i == 0x2c) || (i == 0x2d))) || ((i == 0x5f) || ((i >= 0x100) && (i <= 0x24f)))))) || ((((i >= 880) && (i <= 0x7ff)) || ((i >= 0x900) && (i <= 0x18af))) || (((i >= 0x1900) && (i <= 0x1a1f)) || ((i >= 0x1b00) && (i <= 0x1b7f))))) || (((((i >= 0x1e00) && (i <= 0x1fff)) || ((i >= 0x2c00) && (i <= 0x2ddf))) || (((i >= 0x3040) && (i <= 0x312f)) || ((i >= 0x3190) && (i <= 0x31bf)))) || (((((i >= 0x31f0) && (i <= 0x31ff)) || ((i >= 0x4e00) && (i <= 0x9fc3))) || (((i >= 0xa000) && (i <= 0xa4cf)) || ((i >= 0xa720) && (i <= 0xa82f)))) || (((i >= 0xa840) && (i <= 0xa87f)) || ((i >= 0xac00) && (i <= 0xd7af))))))
                {
                    chArray[i] = null;
                }
                else
                {
                    string str = i.ToString(CultureInfo.InvariantCulture);
                    int length = str.Length;
                    char[] chArray2 = new char[length];
                    for (int j = 0; j < length; j++)
                    {
                        chArray2[j] = str[j];
                    }
                    chArray[i] = chArray2;
                }
            }
            return chArray;
        }

        public static string JavaScriptEncode(string input)
        {
            return JavaScriptEncode(input, true);
        }

        public static string JavaScriptEncode(string input, bool emitQuotes)
        {
            if (string.IsNullOrEmpty(input))
            {
                if (!emitQuotes)
                {
                    return string.Empty;
                }
                return "''";
            }
            int length = 0;
            int num2 = input.Length;
            char[] chArray = new char[num2 * 8];
            if (emitQuotes)
            {
                chArray[length++] = '\'';
            }
            for (int i = 0; i < num2; i++)
            {
                int index = input[i];
                char ch = input[i];
                if (((SafeListCodes[index] != null) || (index == 0x5c)) || ((index >= 0x7b) && (index <= 0x7f)))
                {
                    if (index >= 0x7f)
                    {
                        chArray[length++] = '\\';
                        chArray[length++] = 'u';
                        int num5 = ch;
                        string str = num5.ToString("x", CultureInfo.InvariantCulture).PadLeft(4, '0');
                        chArray[length++] = str[0];
                        chArray[length++] = str[1];
                        chArray[length++] = str[2];
                        chArray[length++] = str[3];
                    }
                    else
                    {
                        chArray[length++] = '\\';
                        chArray[length++] = 'x';
                        int num6 = ch;
                        string str2 = num6.ToString("x", CultureInfo.InvariantCulture).PadLeft(2, '0');
                        chArray[length++] = str2[0];
                        chArray[length++] = str2[1];
                    }
                }
                else
                {
                    chArray[length++] = input[i];
                }
            }
            if (emitQuotes)
            {
                chArray[length++] = '\'';
            }
            return new string(chArray, 0, length);
        }

        public static string LdapDistinguishedNameEncode(string input)
        {
            return LdapDistinguishedNameEncode(input, true, true);
        }

        public static string LdapDistinguishedNameEncode(string input, bool useInitialCharacterRules, bool useFinalCharacterRule)
        {
            return LdapEncoder.DistinguishedNameEncode(input, useInitialCharacterRules, useFinalCharacterRule);
        }

        [Obsolete("This method has been deprecated. Please use Encoder.LdapFilterEncode() instead.")]
        public static string LdapEncode(string input)
        {
            return LdapFilterEncode(input);
        }

        public static string LdapFilterEncode(string input)
        {
            return LdapEncoder.FilterEncode(input);
        }

        public static string UrlEncode(string input)
        {
            return UrlEncode(input, Encoding.UTF8);
        }

        public static string UrlEncode(string input, int codePage)
        {
            return UrlEncode(input, Encoding.GetEncoding(codePage));
        }

        public static string UrlEncode(string input, Encoding inputEncoding)
        {
            if (inputEncoding == null)
            {
                inputEncoding = Encoding.UTF8;
            }
            return HtmlParameterEncoder.QueryStringParameterEncode(input, inputEncoding);
        }

        public static string UrlPathEncode(string input)
        {
            return UrlEncoder.PathEncode(input);
        }

        public static string VisualBasicScriptEncode(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return "\"\"";
            }
            int length = 0;
            int num2 = input.Length;
            char[] chArray = new char[num2 * 12];
            bool flag = false;
            for (int i = 0; i < num2; i++)
            {
                int index = input[i];
                char ch = input[i];
                if (SafeListCodes[index] != null)
                {
                    if (flag)
                    {
                        chArray[length++] = '"';
                        flag = false;
                    }
                    foreach (char ch2 in "&chrw(" + ((uint) ch) + ")")
                    {
                        chArray[length++] = ch2;
                    }
                }
                else
                {
                    if (!flag)
                    {
                        chArray[length++] = '&';
                        chArray[length++] = '"';
                        flag = true;
                    }
                    chArray[length++] = input[i];
                }
            }
            if (flag)
            {
                chArray[length++] = '"';
            }
            if ((chArray.Length > 0) && (chArray[0] == '&'))
            {
                return new string(chArray, 1, length - 1);
            }
            return new string(chArray, 0, length);
        }

        public static string XmlAttributeEncode(string input)
        {
            return UnicodeCharacterEncoder.XmlAttributeEncode(input);
        }

        public static string XmlEncode(string input)
        {
            return UnicodeCharacterEncoder.XmlEncode(input);
        }
    }
}

